package com.example.exp_5;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TeacherBpo {
    public static void insert(Context context,Teacher teacher) {
        SQLHelper sqlHelper = new SQLHelper(context,"teacher.db",null,1);
        SQLiteDatabase db = sqlHelper.getReadableDatabase();
        if (teacher.getNumber().equals("")) {
            Toast.makeText(context.getApplicationContext(), "请输入教师号",Toast.LENGTH_SHORT).show();
        } else if (teacher.getName().equals("")) {
            Toast.makeText(context.getApplicationContext(), "请输入姓名",Toast.LENGTH_SHORT).show();
        } else if (teacher.getSex().equals("")) {
            Toast.makeText(context.getApplicationContext(), "请输入性别",Toast.LENGTH_SHORT).show();
        } else if (teacher.getSalary().equals("")) {
            Toast.makeText(context.getApplicationContext(), "请输入工资",Toast.LENGTH_SHORT).show();
        } else {
            String insertSQL = "insert into teacher(number,name,sex,salary) values(?,?,?,?)";
            db.execSQL(insertSQL, new Object[]{teacher.getNumber(),teacher.getName(),teacher.getSex(),teacher.getSalary()});
            db.close();
            Toast.makeText(context.getApplicationContext(), "添加成功",Toast.LENGTH_SHORT).show();
        }
    }
    public static void delete(Context context,String number) {
        SQLHelper sqlHelper = new SQLHelper(context,"teacher.db",null,1);
        SQLiteDatabase db = sqlHelper.getReadableDatabase();
        if (number.equals("")) {
            Toast.makeText(context.getApplicationContext(), "请选择一项进行删除",Toast.LENGTH_SHORT).show();
        } else {
            String deleteSQL = "delete from teacher where number = ?";
            db.execSQL(deleteSQL,new String[]{number});
            db.close();
            Toast.makeText(context.getApplicationContext(), "删除成功",Toast.LENGTH_SHORT).show();
        }
    }
    public static void update(Context context,Teacher teacher,Teacher oldTeacher) {
        SQLHelper sqlHelper = new SQLHelper(context,"teacher.db",null,1);
        SQLiteDatabase db = sqlHelper.getReadableDatabase();
        String updateSQL = "update teacher set number = ?,name = ?,sex = ?,salary = ? where number = ?";
        if (oldTeacher.getNumber().equals("")) {
            Toast.makeText(context.getApplicationContext(), "请选择需要修改的数据",Toast.LENGTH_SHORT).show();
        }
        else {
            if (teacher.getNumber().equals("")) {
                teacher.setNumber(oldTeacher.getNumber());
            }
            if (teacher.getName().equals("")) {
                teacher.setName(oldTeacher.getName());
            }
            if (teacher.getSex().equals("")) {
                teacher.setSex(oldTeacher.getSex());
            }
            if (teacher.getSalary().equals("")) {
                teacher.setSalary(oldTeacher.getSalary());
            }
            db.execSQL(updateSQL,new Object[]{teacher.getNumber(),teacher.getName(),teacher.getSex(),teacher.getSalary(),oldTeacher.getNumber()});
            db.close();
            Toast.makeText(context.getApplicationContext(), "更新成功",Toast.LENGTH_SHORT).show();
        }
    }
    public static List<Map<String,String>> search(Context context,Teacher teacher) {
        List<Map<String, String>> teacherList = new ArrayList<>();
        SQLHelper sqlHelper = new SQLHelper(context, "teacher.db", null, 1);
        SQLiteDatabase db = sqlHelper.getReadableDatabase();
        String searchSQL = "select number,name,sex,salary from teacher where number like ? and name like ? and sex like ?";
        @SuppressLint("Recycle") Cursor cursor = db.rawQuery(searchSQL, new String[]{"%" + teacher.getNumber() + "%", "%" + teacher.getName() + "%", "%" + teacher.getSex() + "%"});
        while (cursor.moveToNext()) {
            Map<String, String> map = new HashMap<>();
            map.put("number", cursor.getString(0));
            map.put("name", cursor.getString(1));
            map.put("sex", cursor.getString(2));
            map.put("salary", cursor.getString(3));
            teacherList.add(map);
        }
        return teacherList;
    }

    public static List<Map<String,String>> getAllTeachers (Context context) {
        List<Map<String, String>> teacherList = new ArrayList<>();
        SQLHelper sqlHelper = new SQLHelper(context, "teacher.db", null, 1);
        SQLiteDatabase db = sqlHelper.getReadableDatabase();
        String selectSQL = "select number,name,sex,salary from teacher";
        @SuppressLint("Recycle") Cursor cursor = db.rawQuery(selectSQL, null);
        while (cursor.moveToNext()) {
            Map<String, String> map = new HashMap<>();
            map.put("number", cursor.getString(0));
            map.put("name", cursor.getString(1));
            map.put("sex", cursor.getString(2));
            map.put("salary", cursor.getString(3));
            teacherList.add(map);
        }
        return teacherList;
    }
}
